import {PageContainer} from '@ant-design/pro-components';
import {useModel} from '@umijs/max';
import {Card, theme} from 'antd';
import React, { useEffect, useState } from 'react';

import {ProList} from '@ant-design/pro-components';
import {Button, Space, Tag} from 'antd';
import request from 'umi-request';
import { message } from 'antd';
import {FormattedMessage} from "@@/exports";
import {listInterfaceInfoByPageUsingGET} from "@/services/tuzi-server/interfaceInfoController";

const Index: React.FC = () => {

  const [loading, setLoading] = useState(false);
  const [list, setList] = useState<API.InterfaceInfo[]>([]);
  const [total, setTotal] = useState<number>(0);
  const [defaultPageSize] = useState<number>(5);

  const loadData = async (current = 1, pageSize = defaultPageSize) => {
    setLoading(true);
    try {
      const res = await listInterfaceInfoByPageUsingGET({
        current,
        pageSize,
      });
      setList(res?.data?.records ?? []);
      setTotal(res?.data?.total ?? 0);
    } catch (error: any) {
      message.error('请求失败，' + error.message);
    }
    setLoading(false);
  };

  useEffect(() => {
    loadData();
  }, []);

  return (
    <ProList<API.InterfaceInfo>

      search={{}}
      rowKey="name"
      loading={loading}
      headerTitle={
        <FormattedMessage
          id="index.list-title"
          defaultMessage="接口列表"
        />
      }
      dataSource={list}
      pagination={{
        showTotal(total: number) {
          return '总数：' + total;
        },
        pageSize: defaultPageSize,
        total,
        onChange(page, pageSize) {
          loadData(page, pageSize);
        },
      }}
      showActions="hover"
      metas={{
        title: {
          dataIndex: 'name',
          title: () => {
            return (
              <FormattedMessage
                id="index.interfaceInfo-name"
                defaultMessage="接口名称"
              />
            );
          },
        },
        description: {
          dataIndex: 'description',
          title: () => {
            return (
              <FormattedMessage
                id="index.interfaceInfo-content"
                defaultMessage="接口内容"
              />
            );
          },
        },
        actions: {

          render: (text, row) => [
            <a
              href={"/interfaceInfo/" + row.id}
              target="_blank"
              rel="noopener noreferrer"
              key={row.id}
            >
              <FormattedMessage
                id="index.list-actions-view"
                defaultMessage="查看"
              />
            </a>,
          ],
          search: false,
        },
      }}
    />
  );
};

export default Index;
